#include "InnerRegion.h"
#include "SentinelBridgeSpace.h"
#include "PortalOfPowerSpace.h"
#include "InstructionFactory.h"
#include "Space.h"

namespace talisman_map
{

InnerRegion::InnerRegion(){}

void InnerRegion::generateRegion(PortalOfPowerSpace * iPortalOfPowerSpace, vector<std::string> &spaces)
{
	Space * wStartSpace = new Space();
	wStartSpace->setDescription("Mines - Roll to decide your fate!");
	setStartSpace(wStartSpace);

	Space * wNextSpace;
	Space * wPrevSpaceOfNextSpace = wStartSpace;

	for (unsigned i = 0; i < spaces.size(); ++i)
	{
		wNextSpace = new Space();
		
		if (spaces[i].find('/') != std::string::npos)
		{
			std::string wDescr = spaces[i].substr(0, spaces[i].find('/'));
			std::string wInstructionName = spaces[i].substr(wDescr.length()+1);
			Instruction * wInstruction = InstructionFactory::instructionFromString(wInstructionName);
			wNextSpace->setInstruction(wInstruction);
			wNextSpace->setDescription(wDescr);
		}
		else
		wNextSpace->setDescription(spaces[i]);

		wNextSpace->setPrevSpace(wPrevSpaceOfNextSpace);
		wPrevSpaceOfNextSpace->setNextSpace(wNextSpace);
		wPrevSpaceOfNextSpace = wNextSpace;
	}

	// Insert the bridge
	wNextSpace = iPortalOfPowerSpace;
	wNextSpace->setPrevSpace(wPrevSpaceOfNextSpace);
	wPrevSpaceOfNextSpace->setNextSpace(wNextSpace);

	wNextSpace->setNextSpace(wStartSpace);
	wStartSpace->setPrevSpace(wNextSpace); // Now totally circular

	// Decorate
	getStartSpace()->setColor(Qt::red);
	getStartSpace()->setPicture("Images\\space3-inner.gif");
	for (Space * sp = getNextSpace(getStartSpace()); sp != getStartSpace(); sp = getNextSpace(sp))
	{
		sp->setColor(Qt::red);

		PortalOfPowerSpace *pop = dynamic_cast<PortalOfPowerSpace *>(sp);
		SentinelBridgeSpace *sb = dynamic_cast<SentinelBridgeSpace *>(sp);
		if (0 == pop && 0 == sb)
			sp->setPicture("Images\\space3-inner.gif");
	}
}
}

